import { createHmac } from 'crypto';
export function datetime() {
    const rfc123Date = new Date()
    return toRfc123(rfc123Date)
}

function toRfc123(date) {
    const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
    const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    let day = days[date.getUTCDay()];
    let dateNum = date.getUTCDate();
    let month = months[date.getUTCMonth()];
    let year = date.getUTCFullYear();
    let hours = date.getUTCHours();
    let minutes = date.getUTCMinutes();
    let seconds = date.getUTCSeconds();
    return `${day}, ${dateNum} ${month} ${year} ${pad(hours)}:${pad(minutes)}:${pad(seconds)} GMT`;
    function pad(num) {
        return num < 10 ? '0' + num : num;
    }
}

export function toAiHost() {
    let tmp = "host: " + "spark-api.xf-yun.com" + "\n"
    tmp += "date: " + datetime() + "\n"
    tmp += "GET " + "/v1.1/chat" + "HTTP/1.1"
    return tmp
}

export function utf8(str) {
    return Buffer.from(str, 'utf-8') as any as string;
}

export function base64(str) {
    return Buffer.from(str).toString('base64')
}

export function hmac(key, content) {
    return createHmac('sha256', key).update(content).digest('base64')
}


